/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Alumno;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Matricula;
import com.sistema.gestionuniversitaria.BusinessLayer.Profesor;
import com.sistema.gestionuniversitaria.dataLayer.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Max
 */
public class MostrarAlumnosMatricula extends javax.swing.JInternalFrame {

    /**
     * Creates new form MostrarAlumnosMatricula
     */
    CarreraData caData = new CarreraData();
    ProfesorData proData = new ProfesorData();
    CursoData curData = new CursoData();
    AlumnoData alData = new AlumnoData();
    MatriculaData matData = new MatriculaData();
    DefaultTableModel modelo;
    String cedula;

    public MostrarAlumnosMatricula(String sigla, String nombreCurso, String cedula) throws ExcepcionGlobal, NoDataException, SQLException {
        initComponents();
        this.cedula = cedula;
        jTbxSigla.setText(sigla);
        jTbxNombre.setText(nombreCurso);
        jTbxProfesor.setText(getNombre());
        llenarTabla();
    }

    public String getNombre() throws ExcepcionGlobal, NoDataException {
        Profesor p = proData.getProfesorPorCedula(cedula);
        return p.getNombre() + " " + p.getApellidos();
    }

    private void llenarTabla() throws SQLException, ExcepcionGlobal, NoDataException {

        modelo = new DefaultTableModel();
        ArrayList<Matricula> listaAlumnos = new ArrayList<Matricula>();
        listaAlumnos = matData.getAlumnosCurso(jTbxSigla.getText());
        modelo.setColumnCount(0);
        modelo.addColumn("Cédula");
        modelo.addColumn("Nombre");
        modelo.addColumn("Apellidos");
        modelo.addColumn("Email");
        modelo.addColumn("Nota");
        modelo.addColumn("Estado");
        modelo.setNumRows(listaAlumnos.size());
        int contador = 0;
        for (Matricula matr : listaAlumnos) {
            modelo.setValueAt(matr.getAlumno().getCedula(), contador, 0);
            modelo.setValueAt(matr.getAlumno().getNombre(), contador, 1);
            modelo.setValueAt(matr.getAlumno().getApellidos(), contador, 2);
            modelo.setValueAt(matr.getAlumno().getEmail(), contador, 3);
            modelo.setValueAt(matr.getNota(), contador, 4);
            modelo.setValueAt(matr.getEstado(), contador, 5);
            contador = contador + 1;
        }

        jTableAlumnos.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTbxSigla = new javax.swing.JTextField();
        jTbxNombre = new javax.swing.JTextField();
        jTbxProfesor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableAlumnos = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Listado de Alumnos");

        jLabel1.setText("Sigla:");

        jLabel2.setText("Curso:");

        jLabel3.setText("Profesor:");

        jTbxSigla.setEnabled(false);

        jTbxNombre.setEnabled(false);

        jTbxProfesor.setEnabled(false);

        jLabel4.setText("Lista de Alumnos:");

        jTableAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane3.setViewportView(jTableAlumnos);

        jButton1.setText("Agregar nota a estudiante");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTbxProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jLabel4)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTbxProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int row = jTableAlumnos.getSelectedRow();
        String cedula, nombre, apellidos;
        cedula = (String) jTableAlumnos.getValueAt(row, 0);
        nombre = (String) jTableAlumnos.getValueAt(row, 1);
        apellidos = (String) jTableAlumnos.getValueAt(row, 2);
        IngresarNotaAlumno ingNota = new IngresarNotaAlumno(cedula, nombre, apellidos, jTbxSigla.getText());
        FramePrincipal.jDsKtFramePrincipal.add(ingNota);
        ingNota.toFront();
        ingNota.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTableAlumnos;
    private javax.swing.JTextField jTbxNombre;
    private javax.swing.JTextField jTbxProfesor;
    private javax.swing.JTextField jTbxSigla;
    // End of variables declaration//GEN-END:variables
}
